﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using PJ.Business.Partner;
using PJ.Business;
using Microsoft.Practices.Composite.Events;
using System.Windows;

namespace PJ.Presentation.Partner
{
    public abstract class PartnerModel : ItemViewModel, IPartnerModel
    {
        public PartnerModel()
        {
        }

        public PartnerModel(string globalId, IPartnerService service)
            : base(globalId, service)
        {
        }

        public PartnerModel(IBusinessObject obj, IPartnerService service)
            : base(obj, service)
        {
        }

        public PartnerModel(IView view, IEventAggregator aggregator, IPartnerService service)
            : base(view, aggregator, service)
        {
        }

        /// <summary>
        /// GetValues. Sets the businness object properties using the model property values
        /// </summary>
        public override IBusinessObject GetValues()
        {
            if (base.GetValues() == null)
            {
                if (IsStringNull(Phone) && IsStringNull(Address))
                    return null;
            }
            BusinessObject.Phone = Phone;
            BusinessObject.Address = Address;
            return BusinessObject;
        }
        /// <summary>
        /// SetValues. Sets the model properties using the businessobject property values
        /// </summary>
        /// <param name="businessObject"></param>
        public override void SetValues(IBusinessObject businessObject, bool saveBefore)
        {
            base.SetValues(businessObject, saveBefore);
            if (businessObject == null)
            {
                Phone = null;
                Address = null;
            }
            else
            {
                Phone = BusinessObject.Phone;
                Address = BusinessObject.Address;
            }
        }

        public string Phone
        {
            get { return (string)ThreadUnsafeGetValue(PhoneProperty); }
            set { ThreadUnsafeSetValue(PhoneProperty, value); }
        }

        public static readonly DependencyProperty PhoneProperty =
            DependencyProperty.Register("Phone", typeof(string), typeof(PartnerModel), new UIPropertyMetadata(""));

        public string Address
        {
            get { return (string)ThreadUnsafeGetValue(AddressProperty); }
            set { ThreadUnsafeSetValue(AddressProperty, value); }
        }

        public static readonly DependencyProperty AddressProperty =
            DependencyProperty.Register("Address", typeof(string), typeof(PartnerModel), new UIPropertyMetadata(""));

        protected override bool ValuesChanged()
        {
            if (base.ValuesChanged())
                return true;
            else
            {
                if (IsBusinessObjectNull)
                {
                    if (IsStringNull(Phone) && IsStringNull(Address))
                        return false;
                }
                if (StringEqual(Phone, BusinessObject.Phone) && StringEqual(Address, BusinessObject.Address))
                    return false;
                else
                    return true;
            }
        }

        protected new IPartner BusinessObject { get { return base.BusinessObject as IPartner; } }

    }
}
